package com.youlu.campus.web.complaint;

import com.youlu.campus.wechat.service.impl.MerchantComplaintService;
import com.youlu.campus.wechat.service.WechatComplaintService;
import com.youlu.campus.wechat.entity.ComplaintNotifyResponse;
import com.youlu.campus.wechat.facotrys.WechatComplaintServiceFacotry;
import com.youlu.campus.entity.WechatMerchantConfig;
import com.youlu.campus.service.wechat.WechatMerchantConfigService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
public class YiZuMerchantController {
    @Resource
    private MerchantComplaintService merchantComplaintService;

    @Resource
    private WechatMerchantConfigService configService;

    @Resource
    private WechatComplaintServiceFacotry facotry;

    /**
     * 商户投诉 url 路径配置
     *
     * @return
     */
    @PostMapping(value = "/public/complaint-notify/create-url")
    public QResult notifyUrla(@RequestBody WechatMerchantConfig param) {

        WechatMerchantConfig config = configService.findByMerchantIdAndDecrypt(param.getMerchantId());
        WechatComplaintService wechatComplaintService = facotry.getWechatComplaintService(config);

        ComplaintNotifyResponse notifyUrl = wechatComplaintService.getNotifyUrl();
        log.info("complaintNotify notifyUrl:  {}", notifyUrl);
        wechatComplaintService.deleteNotifyUrl();

        wechatComplaintService.createNotifyUrl(param.getNotifyUrl());
        notifyUrl = wechatComplaintService.getNotifyUrl();
        log.info("complaintNotify notifyUrl new:  {}", notifyUrl);

        return QResult.success(notifyUrl);
    }


    /**
     * 商户投诉通知消息 处理
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/public/complaint-notify/notify")
    public Map<String, Object> complaintNotify(HttpServletRequest request, @RequestBody(required = false) String param) {
        log.info("complaintNotify param:{}", param);
        Map<String, Object> stringObjectMap = merchantComplaintService.complaintNotify2(request, param);
        log.info("complaintNotify result:{}", stringObjectMap);
        return stringObjectMap;
    }

    /**
     * 商户号通知投诉处理
     * 亿族科技-1716534148
     *
     * @param request
     * @return
     */
    @PostMapping("/public/complaint-notify/yizu/1716534148")
    public Map<String, Object> notify1716534148(HttpServletRequest request) {
        return merchantComplaintService.complaintNotify(request);
    }

    /**
     * 商户号通知投诉处理
     * 亿族科技-1718205834
     *
     * @param request
     * @return
     */
    @PostMapping("/public/complaint-notify/yizu/1718205834")
    public Map<String, Object> notify1718205834(HttpServletRequest request) {
        return merchantComplaintService.complaintNotify(request);
    }
}
